const http = require('http');
const url = require('url');
// 引入事件类
const EventEmiter = require('events');

// 实例化自定义事件对象
let routeEvent = new EventEmiter();
// 引入routes路由
let routes = require('./routes.js');
routes(routeEvent);  // 实参

let server = http.createServer((req,res)=>{
    res.setHeader('content-type','text/html;charset=utf-8');
    // 把req.url里真正的地址提取出来
    // /index?name=123 -----> /index
    let urlObj = url.parse(req.url,true);
    console.log(urlObj);
    let pathname = urlObj.pathname;
    routeEvent.emit(pathname,req,res);  // 实参

    // /index  和 /index?name=123 是不是一个地址
    // if (pathname == "/index") {
    //     let data = fs.readFileSync("./views/index.html");
    //     res.write(data)
    // } else if (pathname == "/product") {
    //     let data = fs.readFileSync("./views/product.html");
    //     res.write(data)
    // } else if (pathname == "/users") {
    //     let data = fs.readFileSync("./views/users.html");
    //     res.write(data);
    // }

    // res.end();

})

server.listen(8989)